//
//  JOProductViewController.m
//  网易彩票
//
//  Created by song on 15/3/7.
//  Copyright (c) 2015年 song. All rights reserved.
//

#define  JOPRODUCT @"product"

#import "JOProductViewController.h"
#import "JOProduct.h"
#import "JOProductCell.h"

@interface JOProductViewController ()

@end

@implementation JOProductViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UINib *nib = [UINib nibWithNibName:@"JOProductCell" bundle:nil];
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:JOPRODUCT];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:JOPRODUCT];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
}

-(id)init
{
    
    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(100, 100);
    layout.minimumInteritemSpacing =10;
    
    
    layout.sectionInset =UIEdgeInsetsMake(10, 0, 0, 0);
    if(self=[super initWithCollectionViewLayout:layout])
    {
        
    }
    return self;
}

//
//
//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//    
//    return 1;
//}

-(NSArray *)products
{
    if(_products ==nil)
    {
        NSMutableArray *productArray =[NSMutableArray array];
        NSString *path =[[NSBundle mainBundle] pathForResource:@"products.json" ofType:nil];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSArray *dictArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for(NSDictionary *dict in dictArray)
        {
            [productArray addObject:[JOProduct productWithDict:dict]];
        }
        _products = productArray;
    }
    return _products;
}







- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.products.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    JOProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:JOPRODUCT forIndexPath:indexPath];
    
    cell.product = self.products[indexPath.row];
//    cell.backgroundColor =[UIColor redColor];
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
